//
// Created by root on 2019/10/14.
//

#include "AOAManager.h"

#include "AOAaudio.h"
#include "AOADev.h"
#include "Phone.h"

//todo do it later
//static const unsigned char KEYCODE_MEDIA_PLAY_PAUSE[] = {0xCD, 0x00};
static const unsigned char KEYCODE_VOLUME_MUTE[] = {0xE2, 0x00};
//static const unsigned char KEYCODE_VOLUME_UP[] = {0xE9, 0x00};
//static const unsigned char KEYCODE_VOLUME_DOWN[] = {0xEA, 0x00};
//static const unsigned char KEYCODE_MEDIA_NEXT[] = {0xB5, 0x00};
//static const unsigned char KEYCODE_MEDIA_PREVIOUS[] = {0xB6, 0x00};
//static const unsigned char KEYCODE_MEDIA_STOP[] = {0xB7, 0x00};
//static const unsigned char KEYCODE_MUSIC[] = {0x83, 0x01};

void AOAManager::setDev(libusb_device *dev, int status) {
    for (auto &mode : m_mode) {
        if (mode->IsBelong(dev, status)) {
            mode->handle(dev, status, this);
            break;
        }
    }
}

AOAManager::AOAManager() {
    m_mode.emplace_back(new Phone);
    m_mode.emplace_back(new AOADev);
}


AOAManager::~AOAManager() {
    for (auto &mode : m_mode) {
        delete mode;
    }
}

void AOAManager::sentAndroidKey(int key) {
    lock_guard<mutex> l(m_Keylock);
    if (auto aoa = m_wkAoa.lock()) {
        aoa->sendKey(KEYCODE_VOLUME_MUTE);
    } else {
        cout << " the aoa is gone " << endl;
    }
}

void AOAManager::deattachAoaDevice() {
    lock_guard<mutex> l(m_Keylock);
    m_wkAoa.reset();
}

void AOAManager::attachAoaDevice(weak_ptr<AOAaudio> aoa) {
    lock_guard<mutex> l(m_Keylock);
    m_wkAoa = aoa;
}



